package com.zsrt.java.day20170109.lh.daily.demo03;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.junit.Test;

/**
 * 以后凡是看到空指针异常 NullPointException 
 * 确定一个不为空的属性放在前面
 * @author My
 *
 */
public class ArrayListTest {
	
	@Test
	public void add() throws Exception {
		
		List<Student> studentList=new ArrayList<Student>();
		Student lyh=new Student();
		lyh.setName("小宏");
		lyh.setPhone("15989309781");
		lyh.setAge(24);
		
		Student lh=new Student();
		lh.setName("刘浩");
		lh.setPhone("15766772116");
		lh.setAge(24);
		
		studentList.add(lyh);
		studentList.add(lh);
		System.out.println(studentList.size());
		
		
		System.out.println("==============修改之前==================");
		for(int index=0;index<studentList.size();index++){
			System.out.println(studentList.get(index));
		}
		
		//当我们把两个学生增加到集合后 发现刘浩的手机号码写错了 把刘浩手机号码修改正确的
		
		//1遍历集合
		
		for(int i=0;i<studentList.size();i++){
			
			//拿到每一个学生，看姓名是否等于刘浩
			if("刘浩".equals(studentList.get(i).getName())){
				
				//如果等于刘浩 把刘浩的手机号码改成正确的号码  
				studentList.get(i).setPhone("110");
			}
			//如果不是 继续遍历
			continue;
		}
		
		System.out.println("================================");
		for (Student st : studentList) {
			System.out.println(st);
			
		}
		
		
		System.out.println("================================");
		Iterator<Student> it = studentList.iterator();
		while(it.hasNext()){
			Student stu = it.next();
			System.out.println(stu);
		}
	
		System.out.println("================================");
		for(int index=0;index<studentList.size();index++){
			System.out.println(studentList.get(index));
		}
		
		System.out.println("=============it1.remove()===================");
		for(Iterator<Student> it1=studentList.iterator();it1.hasNext();){
			Student student1 = it1.next();
			
			it1.remove();
			System.out.println(student1);
			
		}
		
		
		
		
		
		
		
		
	}
	
	
	@Test
	public void update() throws Exception {
		
		List<Student> studentList=new ArrayList<Student>();
		Student lyh=new Student();
		lyh.setName("小宏");
		lyh.setPhone("15989309781");
		lyh.setAge(24);
		
		Student lh=new Student();
		lh.setName("刘浩");
		lh.setPhone("15766772116");
		lh.setAge(24);
		
		studentList.add(lyh);
		studentList.add(lh);
		System.out.println(studentList.size());
		
		
		System.out.println("==============修改之前==================");
		for(int index=0;index<studentList.size();index++){
			System.out.println(studentList.get(index));
		}
		
		for(int i=0;i<studentList.size();i++){
			Student student = studentList.get(i);
			
			//调用方法  不是基本类型的变量传的是地址
			changeStudent("刘浩", "110", 18, student);
		}
		
		System.out.println("================================");
		for (Student st : studentList) {
			System.out.println(st);
			
		}
	}
	
	
	//需求，随你给一 个姓名 然后我可以修改这个名字对应的手机号码和年龄
	/**
	 * 
	 * @param name 要修改的对象
	 * @param newPhone 新手机号码
	 * @param newage 新年龄
	 * @param stu  学生
	 */
	public void changeStudent(String name,String newPhone,int newage,Student stu){
		
		//如果名字相等就修改
		if(name.equals(stu.getName())){
			stu.setPhone(newPhone);
			stu.setAge(newage);
		}
		
	}
}
